/*
 * Creation : 20/08/2013
 */
/**
 * 
 */
package com.jam.sqlite;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

/**
 * This class execute the query and executions for sqlite db.
 * 
 * @author Fernando Ariel Perelli.
 * 
 */
public abstract class SQLiteExecutions {

	@SuppressWarnings("null")
	public Collection<Object> executeQuery(String query) throws ClassNotFoundException, SQLException {
		Connection connection = null;
		Collection<Object> collection = null;
		try {
			connection = SQLiteConnection.getIntance().getConnection();
			Statement statement = connection.createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				collection.add(initObjet(resultSet));
			}

			resultSet.close();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connection.close();
		}
		return collection;
	}

	protected abstract Collection<Object> initObjet(ResultSet resultSet);

	public void create(String insert, Object object) throws ClassNotFoundException, SQLException {
		Connection connection = null;
		try {
			connection = SQLiteConnection.getIntance().getConnection();
			PreparedStatement statement = connection.prepareStatement(insert);
			setParameters(statement, object);
			statement.addBatch();
			int[] i = statement.executeBatch();

			for (int j : i) {
				if (j == Statement.EXECUTE_FAILED) {
					throw new SQLException();
				}
			}
			connection.setAutoCommit(true);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connection.close();
		}
	}

	protected abstract String[] setParameters(PreparedStatement statement, Object object);

	public void update(String update, Object object) throws ClassNotFoundException, SQLException {
		Connection connection = null;
		try {
			connection = SQLiteConnection.getIntance().getConnection();
			PreparedStatement statement = connection.prepareStatement(update);
			setParameters(statement, object);
			statement.addBatch();
			int[] i = statement.executeBatch();

			for (int j : i) {
				if (j == Statement.EXECUTE_FAILED) {
					throw new SQLException();
				}
			}
			connection.setAutoCommit(true);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connection.close();
		}
	}

	public void delete(String delete, Object object) throws ClassNotFoundException, SQLException {
		Connection connection = null;
		try {
			connection = SQLiteConnection.getIntance().getConnection();
			PreparedStatement statement = connection.prepareStatement(delete);
			String sql = prepareDelete(delete, object);
			statement.executeUpdate(sql);
			connection.setAutoCommit(true);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connection.close();
		}
	}

	protected abstract String prepareDelete(String delete, Object object);

	protected abstract String prepareUpdate(String update, Object object);

}
